angular.module("app").controller("user.manageInfo.Ctrl", ["$scope", "$stateParams", "User", function ($scope, $stateParams, User) {
    $scope.roleDict = angular.copy($scope.$parent.roleDict);
    if ($stateParams.manageInfo) {
        $scope.manageInfo = $stateParams.manageInfo;
        $scope.manageInfo.roles.split(",").forEach(function (item) {
            var tmp = $scope.roleDict[item.replace(/[^\d]+/g, "")];
            if (tmp) {
                tmp.checked = true;
            }
        });
    }
    else if ($stateParams.manageId) {
        User.getUserInfo({}, { userId: $stateParams.manageId }, function (data) {
            if (data.statusCode == 0) {
                $scope.manageInfo = data.data;
                $scope.manageInfo.roles.split(",").forEach(function (item) {
                    var tmp = $scope.roleDict[item.replace(/[^\d]+/g, "")];
                    if (tmp) {
                        tmp.checked = true;
                    }
                });
            } else {
                alert(data.statusMessage)
            }
        })
    }
    else {
        $scope.manageInfo = {};
    }

    $scope.type = $stateParams.type;
    $scope.types = { create: "创建", edit: "编辑", view: "查看" }
    $scope.roles = {};
    $scope.addRole = function (roleId, item, event) {
        if (event.currentTarget.checked) {
            $scope.roles[roleId] = "{" + roleId + "}";
            item.checked = true;
        }
        else {
            delete $scope.roles[roleId];
            item.checked = false;
        }
    };
    $scope.saveManageInfo = function () {
        var manageInfo = $scope.manageInfo;
        if ($scope.type == "create" && manageInfo.secondPassword != manageInfo.password) {
            alert("两次密码不一致,请重新输入!");
            return;
        }
        manageInfo.roles = "";
        for (var key in $scope.roleDict) {
            if ($scope.roleDict[key].checked) {
                manageInfo.roles += "{" + key + "}" + ",";
            }
        }
        delete manageInfo.secondPassword;
        if ($scope.type == "edit") {
            delete manageInfo.password;
        }
        User.userEdit({}, manageInfo, function (data) {
            if (data.statusCode == 0) {
                alert("更新成功!");
                $scope.goBack();
            } else {
                alert(data.statusMessage)
            }
        })
    };
}]);